init_ipaddress.js ➔ grepSDP   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 15
rs 9.4285
1
/*
2
* @Author: jdi-juma
3
* @Date:   2017-12-09 17:11:58
4
* @Last Modified by:   jdi-juma
5
* @Last Modified time: 2017-12-09 22:07:37
6
*/
7
// NOTE: window.RTCPeerConnection is "not a constructor" in FF22/23
8
var RTCPeerConnection = /*window.RTCPeerConnection ||*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
9
10
if (RTCPeerConnection) (function () {
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
11
    var rtc = new RTCPeerConnection({iceServers:[]});
12
    if (1 || window.mozRTCPeerConnection) {      // FF [and now Chrome!] needs a channel/stream to proceed
13
        rtc.createDataChannel('', {reliable:false});
14
    };
15
    
16
    rtc.onicecandidate = function (evt) {
17
        // convert the candidate to SDP so we can run it through our general parser
18
        // see https://twitter.com/lancestout/status/525796175425720320 for details
19
        if (evt.candidate) grepSDP("a="+evt.candidate.candidate);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
20
    };
21
    rtc.createOffer(function (offerDesc) {
22
        grepSDP(offerDesc.sdp);
23
        rtc.setLocalDescription(offerDesc);
24
    }, function (e) { console.warn("offer failed", e); });
25
    
26
    
27
    var addrs = Object.create(null);
28
    addrs["0.0.0.0"] = false;
29
    function updateDisplay(newAddr) {
30
        if (newAddr in addrs) return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
31
        else addrs[newAddr] = true;
32
        var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
33
        //document.getElementById('list').textContent = displayAddrs.join(" or perhaps ") || "n/a";
34
        document.getElementById('list').value = displayAddrs;
35
        if($("meta[name=ipaddress]").attr('content') == '')
36
        {
37
        	$("meta[name=ipaddress]").attr('content', displayAddrs);
38
        }
39
40
    }
41
    
42
    function grepSDP(sdp) {
43
        var hosts = [];
0 ignored issues
show
Unused Code introduced by
The variable hosts seems to be never used. Consider removing it.
Loading history...
44
        sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39
45
            if (~line.indexOf("a=candidate")) {     // http://tools.ietf.org/html/rfc4566#section-5.13
46
                var parts = line.split(' '),        // http://tools.ietf.org/html/rfc5245#section-15.1
47
                    addr = parts[4],
48
                    type = parts[7];
49
                if (type === 'host') updateDisplay(addr);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
50
            } else if (~line.indexOf("c=")) {       // http://tools.ietf.org/html/rfc4566#section-5.7
51
                var parts = line.split(' '),
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable parts already seems to be declared on line 46. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
52
                    addr = parts[2];
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable addr already seems to be declared on line 47. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
53
                updateDisplay(addr);
54
            }
55
        });
56
    }
57
})(); else {
58
59
    document.getElementById('list').value = "<code>ifconfig | grep inet | grep -v inet6 | cut -d\" \" -f2 | tail -n1</code>";
60
    //$('#list').val("<code>ifconfig | grep inet | grep -v inet6 | cut -d\" \" -f2 | tail -n1</code>");
61
    //document.getElementById('list').nextSibling.textContent = "In Chrome and Firefox your IP should display automatically, by the power of WebRTCskull.";
62
}